SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[Ufn_SplitToTable]
(
    @splitString	nvarchar(max),
    @separator		nvarchar(10) = ';'
)
RETURNS @splitStringsTable TABLE
(
	[Value] nvarchar(max)
)
AS
BEGIN
    DECLARE @currentIndex int
    DECLARE @nextIndex int
    DECLARE @returnText nvarchar(max)
    
	SET		@currentIndex=1
    WHILE (@currentIndex <= LEN(@splitString))
    BEGIN
        SELECT @nextIndex = charindex(@separator,@splitString,@currentIndex)
        IF (@nextIndex = 0)
			SET @nextIndex = LEN(@splitString) + 1
		
            SELECT @returnText = substring(@splitString, @currentIndex, @nextIndex - @currentIndex)

			IF (LEN(@returnText) > 0)
				INSERT INTO @splitStringsTable([Value]) VALUES(@returnText)

            SELECT @currentIndex = @nextIndex + 1

	END
    RETURN
END
